package com.ice.base.util;

import com.ice.base.constant.HCNetSDK;
import com.ice.body.hardware.model.DeviceFace;
import com.ice.body.hardware.service.DeviceFaceService;
import com.sun.jna.Pointer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * 人脸识别设备进门布防工具类
 */
@Component
public class FaceOpenBfUtil {

    private Logger log = LoggerFactory.getLogger(FaceOpenBfUtil.class);
    @Autowired
    private FMSGCallBackUtil fmsgCallBackUtil;

    @Resource
    private DeviceFaceService deviceFaceService;

    /**
     * 初始化布防
     * @throws Exception
     */
    public void faceOpenBf(HCNetSDK sdk){
        DeviceFace model = new DeviceFace();
        model.setDeviceType(1);
        ArrayList<DeviceFace> list = deviceFaceService.selectListByModel(model);
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        try{
            HCNetSDK.NET_DVR_DEVICEINFO m_strDeviceInfo= new HCNetSDK.NET_DVR_DEVICEINFO();
            // HCNetSDK sdk = HCNetSDK.INSTANCE;
            list.forEach(face->{
                if(face.getStatus() != 1){
                    return;
                }
                int lUserID = sdk.NET_DVR_Login(face.getIp().trim(),(short)Integer.parseInt(face.getPort().trim()),face.getUserName().trim(),face.getPwd().trim(),m_strDeviceInfo);

                if (lUserID == -1){
                    log.info(face.getIp()+"开门布防用户登录失败,错误码================"+sdk.NET_DVR_GetLastError());
                    return;
                }
                // 启动布防
                SetupAlarmChanOpen(lUserID,-1,sdk);
                log.info(face.getIp()+"+++++++++开门布防success================"+sdk.NET_DVR_GetLastError());
                //sdk.NET_DVR_Logout(lUserID);
            });
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 启动布防
     * @param lUserID·
     */
    private void SetupAlarmChanOpen(int lUserID,int lAlarmHandle,HCNetSDK sdk) {

        // 尚未布防,需要布防
        if (lAlarmHandle< 0){
            Pointer pUser = null;
            if (!sdk.NET_DVR_SetDVRMessageCallBack_V31(fmsgCallBackUtil, pUser)){
                log.info("设置回调函数失败!");
            }

            HCNetSDK.NET_DVR_SETUPALARM_PARAM m_strAlarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
            m_strAlarmInfo.dwSize=m_strAlarmInfo.size();
            m_strAlarmInfo.byLevel=1;
            m_strAlarmInfo.byDeployType=1;
            m_strAlarmInfo.byAlarmInfoType=1;
            m_strAlarmInfo.write();
            lAlarmHandle = sdk.NET_DVR_SetupAlarmChan_V41(lUserID, m_strAlarmInfo);
            if (lAlarmHandle == -1){
                int result = sdk.NET_DVR_GetLastError();
                log.info("开门人脸识别设备布防失败,错误码================"+result);
            }else{
                log.info("开门人脸识别设备布防成功");
            }
        }
    }

}